home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
RTF
/
test
/
rt_test.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
3KB
|
149 lines
/* rt_test.c
*
* Test driver for RichText widget.
*/
#include <Xm/Xm.h>
#include <Xm/ScrolledW.h>
#include <Xm/FileSB.h>
#include "RichText.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
static void
clickCB(Widget caller, XtPointer dummy, XtPointer cbd)
{
XcRichTextCallbackStruct* rt = (XcRichTextCallbackStruct*)cbd;
printf("selection: %.*s\n", rt->point - rt->mark, rt->value + rt->mark);
printf("next 10 chars: %.10s\n", rt->value + rt->point);
if(rt->point == rt->mark){
Boolean rtf;
XtVaGetValues(caller, XcNrtf, &rtf, NULL);
printf("RichText interpretation: %s\n", rtf ? "off" : "on");
XtVaSetValues(caller, XcNrtf, !rtf, NULL);
}
}
static void resizeHandler(Widget w, XtPointer pt, XtPointer motifdata)
{
Widget rt = (Widget)pt;
XmDrawingAreaCallbackStruct* das = (XmDrawingAreaCallbackStruct*)motifdata;
Dimension ow, width;
XtVaGetValues(w,
XmNwidth, &width,
NULL);
/* ignore sizes that are way too small */
if(width > 100)
XtVaSetValues(rt,
XmNwidth, width - 15,
NULL);
}
static void showfile(Widget w, XtPointer pt, XtPointer md)
{
Widget rt = (Widget)pt;
XmFileSelectionBoxCallbackStruct* fs = (XmFileSelectionBoxCallbackStruct*)md;
FILE* f;
struct stat s;
char* text;
char* fname;
if(!XmStringGetLtoR(fs->value, XmSTRING_DEFAULT_CHARSET, &fname)){
XtWarning("can't XmStringGetLtoR");
return;
}
if(stat(fname, &s)){
XtWarning("can't stat file");
XtFree(fname);
return;
}
f = fopen(fname, "r");
XtFree(fname);
if(!f){
XtWarning("%s: can't open %s\n");
return;
}
if(!(text = (char*)XtMalloc(s.st_size + 1))){
XtWarning("%s: can't malloc enough memory for: %s\n");
return;
}
if(fread(text, sizeof(char), s.st_size, f) != s.st_size){
XtWarning("%s: error reading for: %s\n");
XtFree(text);
return;
}
fclose(f);
text[s.st_size] = 0;
XtVaSetValues(rt,
XcNrtf, text[0] == '{', /* cheap hack test */
XmNvalue, text,
NULL);
}
main(argc, argv)
Cardinal argc; char *argv[];
{
XtAppContext app;
Widget appShell;
Widget rt, sc, clip, fsb;
appShell = XtAppInitialize(&app, "RichTextTest",
NULL, 0,
&argc, argv, NULL,
(ArgList)NULL, 0);
fsb = XmCreateFileSelectionDialog(appShell, "filechooser",
NULL, 0);
XtManageChild(fsb);
sc = XtVaCreateManagedWidget("scroller", xmScrolledWindowWidgetClass,
appShell,
XmNscrollBarDisplayPolicy, XmAS_NEEDED,
XmNscrollingPolicy, XmAUTOMATIC,
NULL);
rt = XtVaCreateManagedWidget("richtext", xcRichTextWidgetClass,
sc,
NULL);
XmScrolledWindowSetAreas(sc, NULL, NULL, rt);
/* In order to allow the user to change the width
of the RichText widget, I trap resize events
on the clip window. */
XtVaGetValues(sc,
XmNclipWindow, &clip,
NULL);
XtAddCallback(clip, XmNresizeCallback, resizeHandler, (XtPointer)rt);
/* show files on request */
XtAddCallback(fsb, XmNokCallback, showfile, (XtPointer)rt);
/* provide some feedback to clicks in the text area */
XtAddCallback(rt, XmNmotionVerifyCallback, clickCB, 0);
XtRealizeWidget ( appShell );
XtAppMainLoop ( app );
return 0;
}